package com.zygh.coordinatestore.service.impl;

import cn.hutool.core.date.DateUtil;
import com.zygh.coordinatestore.mapper.StaffMarkMapper;
import com.zygh.coordinatestore.service.StaffMarkService;
import com.zygh.coordinatestore.util.WktUtils;
import org.postgis.LineString;
import org.postgis.LinearRing;
import org.postgis.Point;
import org.postgis.Polygon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lee
 */
@Service
public class StaffMarkServiceImpl implements StaffMarkService{

    private StaffMarkMapper staffMarkMapper;
    @Autowired
    private void setStaffMarkMapper(StaffMarkMapper staffMarkMapper){
        this.staffMarkMapper = staffMarkMapper;
    }

    /**
     * service实现
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param staffCode 警号
     * @param gpsid gpsid
     * @return List
     */
    @Override
    public List getStaffMark(String startTime,String endTime,String staffCode,String gpsid) {
        List<Map> staff = staffMarkMapper.getFoot(startTime,endTime,staffCode,gpsid);
        if(staff.isEmpty()){
            return null;
        }
        List<Object> list = new ArrayList<>(50);
        Map<String,Object> map = new HashMap<>(3);

        List<Point> gps = staff.stream().map(x -> (Point) x.get("gps")).collect(Collectors.toList());
        LineString linearRing = new LineString(gps.toArray(new Point[]{}));

        String s = WktUtils.wktToJson(linearRing.getTypeString()+ linearRing.getValue());
        map.put("gps",s);

        map.put("staffCode",staff.get(0).get("code"));
        map.put("start",WktUtils.wktToJson(gps.get(0).getTypeString()+gps.get(0).getValue()));
        map.put("end",WktUtils.wktToJson(gps.get(gps.size()-1).getTypeString()+gps.get(gps.size()-1).getValue()));
        list.add(map);
        return list;
    }

    /**
     * 热力图的实现
     * @param areaId 区域id
     * @param sectionId 部门id
     * @param station 部门
     * @return List
     */
    @Override
    public List getHotMark(Integer areaId, String sectionId, String station) {
        return staffMarkMapper.getHot(areaId, sectionId, station);
    }

    /**
     * 圆圈圈选功能的实现
     * @param lng 圆心经度
     * @param lat 圆心纬度
     * @param radius 圆心半径
     * @return List
     */
    @Override
    public List circle(Double lng, Double lat, Double radius) {
        String morning = DateUtil.today()+" 00:00:00";
        String night = DateUtil.today()+" 23:59:59";
        Point point = new Point(lng,lat);
        List<Map<String,Object>> circle = staffMarkMapper.getCircle(point, radius,morning,night);
        for (Map<String,Object> next:circle){
            Point p = (Point) next.get("gps");
            next.put("gps",WktUtils.wktToJson(p.getTypeString()+p.getValue()));
        }
        return circle;
    }

    /**
     * 多边形圈选功能
     * @param lng 经度串
     * @param lat 纬度串
     * @return List
     */
    @Override
    public List polygon(Double[] lng, Double[] lat) {
        String morning = DateUtil.today()+" 00:00:00";
        String night = DateUtil.today()+" 23:59:59";
        if(lng!=null && lng.length>0){
            Point[] p =new Point[lng.length+1];
            for (int i = 0;i<lng.length;i++){
                Point point = new Point(lng[i],lat[i]);
                p[i] = point;
            }
            p[lng.length] = new Point(lng[0],lat[0]);

            LinearRing linearRing = new LinearRing(p);
            Polygon polygon = new Polygon(new LinearRing[]{linearRing});
            List<Map<String,Object>> polygon1 = staffMarkMapper.getPolygon(polygon,morning,night);
            for (Map<String,Object> next:polygon1){
                Point point = (Point) next.get("gps");
                next.put("gps",WktUtils.wktToJson(point.getTypeString()+point.getValue()));
            }
            return polygon1;
        }
        return null;
    }
}
